package com.animee.day15.slice;
import com.animee.day15.ResourceTable;
import com.animee.day15.provider.TabPageProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/** 无限循环播放的页面*/
public class TabAbilitySlice extends AbilitySlice {
    TabList tabList;
    PageSlider slider;

    List<String>titleList;
    List<Component>list = new ArrayList<>();
    private TabPageProvider provider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_tab);
        titleList = Arrays.asList("剧情","喜剧","动作","爱情","科幻","动画","悬疑","恐怖","惊悚","短片");
        initView();
        connTabAndSlider();
    }

    private void connTabAndSlider() {
        //给pageslider设置监听
        slider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }
            @Override
            public void onPageSlideStateChanged(int i) {
            }
            @Override
            public void onPageChosen(int i) {
                  //页面被改变时调用的方法
                tabList.selectTabAt(i);
            }
        });
        //给tablist添加选中监听
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                int position = tab.getPosition();
                slider.setCurrentPage(position);
            }
            @Override
            public void onUnselected(TabList.Tab tab) {
            }
            @Override
            public void onReselected(TabList.Tab tab) {
            }
        });
    }

    private void initView() {
        tabList = findComponentById(ResourceTable.Id_tab_tlist);
        slider = findComponentById(ResourceTable.Id_tab_ps);
        //给tablist 添加tab标签
        for (int i = 0; i < titleList.size(); i++) {
            TabList.Tab tab = tabList.new Tab(this);
            tab.setText(titleList.get(i));
            tabList.addTab(tab);

            Component component = LayoutScatter.getInstance(this)
                    .parse(ResourceTable.Layout_item_tab, null, false);

            Text tv = component.findComponentById(ResourceTable.Id_item_tab_tv);
            tv.setText(titleList.get(i));
            list.add(component);
        }
        tabList.selectTabAt(0);
        //设置pageslider适配器
        provider = new TabPageProvider(this, list);
        slider.setProvider(provider);


    }
}